﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using élénkPróbája.extentions;
using élénkPróbája.Test.dto;
using élénkPróbája.Test.extention;

namespace élénkPróbája.Test
{
    [TestClass]
    public class UserStateTester
    {
        TestValidator validator;

        public UserStateTester()
        {
            validator = new TestValidator();
        }
         
        [TestMethod]
        public void Stores_user_state_against_validation_failure()
        {
            validator.RuleFor(x => x.PublicName).NotNull().WithState(x => "foo");
            var result = validator.Validate(new Company());
            result.Errors.Single().CustomState.ShouldEqual("foo");
        }

        [TestMethod]
        public void Throws_when_provider_is_null()
        {
            MyAssert.Throws<ArgumentNullException>(() => validator.RuleFor(x => x.PublicName).NotNull().WithState(null));
        }

        [TestMethod]
        public void Correctly_provides_object_being_validated()
        {
            Company resultCompany = null;

            validator.RuleFor(x => x.PublicName).NotNull().WithState(x => {
                resultCompany = x;
                return new object();
            });

            var company = new Company();
            validator.Validate(company);

            resultCompany.ShouldBeTheSameAs(company);
        }
    }
}